﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common;
using Common.Events;
using Common.Search;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Events;
using Module1.Models;

namespace Module1.Services
{
    /// <summary>
    /// Dummy data service class. Provides a dummy data model.
    /// Replace with your real data model and/or data service proxy.
    /// </summary>
    public class DataService : IDataService
    {
        private readonly IEventAggregator _eventAggregator;

        public DataService(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;

            // subscribe to the SearchRequestEvent
            _eventAggregator.GetEvent<SearchRequestEvent>().Subscribe(HandleSearchRequest);
        }

        private void HandleSearchRequest(SearchRequestEventData data)
        {
            if (!string.IsNullOrEmpty(data.SearchRequest.RawRequest))
            {
                // performing an exact match against the data items collection (note, assuming only 0 or 1 match here.)
                DataItem item =
                    model.First(di => (string.Compare(di.Name, data.SearchRequest.RawRequest.Trim(), true) == 0));

                if (item != null)
                {
                    // here is the code from MasterViewModel that creates and navigates to a selected Detail view
                    var parameters = new UriQuery();
                    parameters.Add("ID", item.Id);
                    //_regionManager.RequestNavigate(RegionNames.MainRegion,
                    //                                new Uri("EditView" + parameters.ToString(), UriKind.Relative),
                    //                                NavigationCompleted);
                    var itemUri = new Uri("DetailsView" + parameters.ToString(), UriKind.Relative);

                    // Generate the search result for the SearchResponseEvent
                    var result = new SearchResult();
                    result.ResultHeader = "Module1 Results";
                    result.Results = new List<SearchResultItem>()
                                     {
                                         new SearchResultItem()
                                             {
                                                 ItemText = string.Format("'{0}' Details", item.Name),
                                                 ItemUri = itemUri,
                                                 TargetRegion = RegionNames.MainRegion
                                             }
                                     };

                    _eventAggregator.GetEvent<SearchResponseEvent>().Publish(new SearchResponseEventData() { SearchResult = result }); 
                }
            }
        }

        private DataItems model;

        public DataItems GetModel()
        {
            if (model == null)
            {
                model = new DataItems();

                // Dummy Data.
                model.Add(new DataItem() { Id = "1", Name = "Item 1", Description = "Description 1" });
                model.Add(new DataItem() { Id = "2", Name = "Item 2", Description = "Description 2" });
                model.Add(new DataItem() { Id = "3", Name = "Item 3", Description = "Description 3" });
                model.Add(new DataItem() { Id = "4", Name = "Item 4", Description = "Description 4" });
            }

            return model;
        }
    }
}
